package db

import (
	"context"

	"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)

const createUser = `
CREATE (u:User {name: $name, mobile: $mobile, passhash: $passhash})
RETURN u
`

type CreateUserParam struct {
	Name     string `map:"name"`     // string
	Mobile   string `map:"mobile"`   // string
	PassHash string `map:"passhash"` // string
}

func (dao *DAO) CreateUser(ctx context.Context, arg CreateUserParam) (User, error) {
	var i User
	argMap, err := NewParamConverter(&arg).AsMap()
	if err != nil {
		return i, err
	}
	_, err = dao.db.ExecTx(ctx, func(tx NeoTransaction) error {
		result, err := tx.Run(ctx, createUser, argMap)
		if err != nil {
			return err
		}
		err = NewNodeLoader(&i).LoadT(ctx, result, "u")
		if err != nil {
			return err
		}
		dao.db.slog.Infof("i=>%+v", i)
		return nil
	}, neo4j.ExecuteQueryWithDatabase(dao.dbName))

	return i, err
}
